
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'fun/emp/list',
        datatype: "json",
        colModel: [
            { label: '员工ID', name: 'empId', width: 30, key: true},
            { label: '姓名', name: 'empName', width: 100 },
            { label: '身份证号', name: 'idCard', width: 100 },
            { label: '所属部门', name: 'deptName', width: 100 },
            { label: '职名（务）', name: 'office',  width: 100 },
            { label: '状态', name: 'status', width: 50, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span >在岗</span>' :
                        '<span >不在岗</span>';
                }},
            { label: '备注', name: 'remarks',  width: 100 }
        ],
        viewrecords: true,
        height: 400,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            $("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
        },
    });
});
//部门结构树
var dept_ztree;
var dept_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            empName: null,
            deptId:null
        },
        showList: true,
        title: null,
        emp: {},
        empAttachment:{},
        selectDept:'',
        work: {},
        reward: {},
        pickerOptions: {
            disabledDate(time) {
                return time.getTime() > Date.now();
            }
        },
        politicalBackground: [{value: '群众'},{value:'团员'},{value:'预备党员'},{value:'党员'}],
        education: [{value:'大专'},{value:'本科'},{value:'硕士'},{value:'博士'}],
        fileList: [],
        dialogFormVisible: false,
        dialogDeptVisible: false,
        dialog: false,
        drawer: false,
        formLabelWidth: '80px',
        timer: null,
        activeNames: ['1'],
        baseInfo:null,
        workInfo:null,
        formData: [
            {index: 0,  isShow: false}, {index: 11,  isShow: false},
            {index: 1,  isShow: false}, {index: 12,  isShow: false},
            {index: 2,  isShow: false}, {index: 13,  isShow: false},
            {index: 3,  isShow: false}, {index: 14,  isShow: false},
            {index: 4,  isShow: false}, {index: 15,  isShow: false},
            {index: 5,  isShow: false}, {index: 16,  isShow: false},
            {index: 6,  isShow: false}, {index: 17,  isShow: false},
            {index: 7,  isShow: false}, {index: 18,  isShow: false},
            {index: 8,  isShow: false}, {index: 19,  isShow: false},
            {index: 9,  isShow: false}, {index: 20,  isShow: false},
            {index: 10,  isShow: false}, {index: 21,  isShow: false}
        ],
        qualification:[{value: '是'},{value:'否'}],
        employMode:[{value: '编制内'},{value: '合同制'},{value:'外包'}],
        skillGrade:[{value: '一级'},{value:'二级'},{value: '三级'},{value:'四级'},{value: '五级'},{value:'六级'}],
        skillKid:[],
    },

    methods: {
        query: function () {
            vm.reload();
        },
        empShow:function ()
        {
            vm.q.empId = getSelectedRow();
            if(vm.q.empId == null){
                return ;
            }
            window.location.href="emp_show.html?empId="+vm.q.empId;
        },
        update: function (event) {
            var empId = getSelectedRow();
            if(empId == null){
                return ;
            }
            vm.showList = false;
            vm.title = "编辑人员信息";
            vm.getInfo(empId);
           /* vm.reward.empId=empId;
            vm.work.empId=empId;*/
            vm.getDept();
        },
        exportExcel: function (event) {
            var empIds = getSelectedRows();
            if(empIds == null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "fun/emp/exportEmps",
                contentType: "application/json",
                responseType:'blob',
                data: JSON.stringify(empIds)
            }).then(function (r){
                vm.$message('文件内容');
                const link=document.createElement('a');
                let blob=r.data
                link.style.display='none'
                link.href=URL.createObjectURL(blob);
                link.setAttribute('download','测试')
                document.body.appendChild(link)
                link.click()
                document.body.removeChild(link);
            })
            alert('导出位置:G:\\empExcel');
        },
        handleCloseDrawer:function(done) {
            vm.dialog=false;
        },
        cancelForm:function() {
            vm.dialog = false;
        },
        handlePreview:function(file) {
            console.log(file);
        },
        handleRemove:function(file, fileList) {
            console.log(file, fileList);
        },
        fresh:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogFormVisible=false;
        },
        freshDept:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogDeptVisible=false;
        },
        saveOrUpdate: function (event) {
           vm.saveBaseInfo();
         /*  vm.saveWorkInfo();
           vm.saveRewardInfo();*/
        },
        saveBaseInfo:function ()
        {
            let map ={
                'emp':vm.emp,
                'attachment':vm.empAttachment,
                'deptParent':vm.selectDept
            }
            $.ajax({
                    type: "POST",
                    url: baseURL +"fun/emp/update",
                    contentType: "application/json",
                    data: JSON.stringify(map),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                   }
            });
        },
        saveWorkInfo:function ()
        {
            $.ajax({
                    type: "POST",
                    url: baseURL +"fun/emp/updateWork",
                    contentType: "application/json",
                    data: JSON.stringify(vm.work),
                    success: function(r){
                        if(r.code === 0){
                            vm.workInfo=true;
                        }else{
                            vm.workInfo=false;
                        }
                    }
                  });
        },
        saveRewardInfo:function ()
        {
            $.ajax({
                    type: "POST",
                    url: baseURL +"fun/emp/updateReward",
                    contentType: "application/json",
                    data: JSON.stringify(vm.reward),
                    success: function(r) {
                        if (r.code === 0 && vm.workInfo === true && vm.baseInfo === true) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
            });
        },
        getInfo: function(empId){
            vm.formData=[
                {index: 0,  isShow: false}, {index: 11,  isShow: false},
                {index: 1,  isShow: false}, {index: 12,  isShow: false},
                {index: 2,  isShow: false}, {index: 13,  isShow: false},
                {index: 3,  isShow: false}, {index: 14,  isShow: false},
                {index: 4,  isShow: false}, {index: 15,  isShow: false},
                {index: 5,  isShow: false}, {index: 16,  isShow: false},
                {index: 6,  isShow: false}, {index: 17,  isShow: false},
                {index: 7,  isShow: false}, {index: 18,  isShow: false},
                {index: 8,  isShow: false}, {index: 19,  isShow: false},
                {index: 9,  isShow: false}, {index: 20,  isShow: false},
                {index: 10,  isShow: false}, {index: 21,  isShow: false},
            ],
                vm.empAttachment={};
            $.get(baseURL + "fun/emp/info/"+empId, function(r){
                vm.emp = r.emp.empEntity;
                vm.selectDept=r.emp.deptParent;
                switch (vm.selectDept) {
                    case 'jwd':
                        vm.empAttachment= r.emp.jwdEmpEntity;
                        vm.formData[0].isShow=true;
                        break;
                    case 'kyd':
                        vm.empAttachment= r.emp.kydEmpEntity;
                       vm.formData[1].isShow=true;
                        break;
                }
                vm.getDept();
            });
        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
                var node = dept_ztree.getNodeByParam("deptId", vm.emp.deptId);
                if(node != null){
                    dept_ztree.selectNode(node);
                    vm.emp.deptName = node.name;
                }
            })
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.empName,'deptId':vm.q.deptId},
                page:page
            }).trigger("reloadGrid");
        },

    }
});
window.addEventListener("message",function(event){

    vm.showList = true;
    vm.q.deptId=event.data;
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'deptId':event.data,'name': ''},
        page:page
    }).trigger("reloadGrid");
}, false);